package com.sghd.fight.module.game.event;

import com.sghd.common.event.AbstractReceiver;
import com.sghd.common.socket.client.Client;
import com.sghd.common.socket.client.ClientFactory;
import com.sghd.common.socket.core.Message;
import com.sghd.common.socket.handler.filter.firewall.IpFilter;
import com.sghd.fight.module.facade.Receiver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class GameServerEventReceiver extends AbstractReceiver<GameServerEvent> implements IpFilter {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ClientFactory clientFactory;
    /** 大厅连接*/
    private Client client;
    @Autowired
    private Receiver receiver;
    /** 大厅ip*/
    @Value("${hall.ip}")
    private String ip;
    /** 大厅端口*/
    @Value("${hall.port}")
    private int port;

    @Override
    protected void init() {
        super.init();
        client = clientFactory.getClient(ip, port);
    }

    @Override
    public void doEvent(GameServerEvent event) {
        if (event.getCallback() != null) {
            receiver.saveBack(event, event.getCallback());
        }
        Object body = event.getBody();
        client.send(Message.valueOf(event.getCommand(), body));
    }

    @Override
    public String[] getEventNames() {
        return new String[]{GameServerEvent.NAME};
    }

    @Override
    public boolean match(String s) {
        return true;
    }
}
